import api from '../../api';
import _ from 'lodash';

export default {
  namespace: 'tradingCompanyM',
  state: {},
  effects: {
    // 根据查询条件,获取交易对手
    *getTradingCompany({ payload, callback }, { call, put, select }) {
      // 交易对手，数据来源是全市场
      if (payload.dataType == 1) {
        let scenesType = payload.scenesType;
        let res;
        delete payload.dataType;
        delete payload.isOtc;
        delete payload.scenesType;
        if (scenesType == '3' || scenesType == '4') {
          res = yield call(api.getTradingCompany1, { id: payload.value });
        } else {
          res = yield call(api.getTradingCompany, payload);
        }
        if (res && res.code === 0) {
          let dataSource = [];
          if (scenesType == '3' || scenesType == '4') {
            let item = {};
            item.value = res.data?.id;
            item.id = res.data?.id;
            item.title = res.data?.name;
            item.fullName = res.data?.fullName;
            dataSource.push(item);
          } else {
            for (let i = 0; i < res.data.length; i++) {
              if (i >= 100) break; // 取前100条数据
              let item = {};
              item.value = res.data[i].id;
              item.id = res.data[i].id;
              item.title = res.data[i].name;
              item.fullName = res.data[i].fullName;
              dataSource.push(item);
            }
          }

          callback(dataSource);
        }
      }
      // 交易对手，数据来源是交易对手模块
      if (payload.dataType == 2) {
        // 该接口与交易对手基础信息页面获取表格是同一个接口，这边只按短名来模糊查询
        let query;
        if (payload.isOtc) {
          query = { currentPage: 1, numInPage: 10000, shortName: payload.value, isOtc: payload.isOtc };
        } else {
          query = { currentPage: 1, numInPage: 10000, shortName: payload.value };
        }
        const res = yield call(api.counterpartyInformation.queryCounterpartyList, query);
        if (res && res.code == 0) {
          let dataSource = [];
          for (let i = 0; i < res.data.dataSource.length; i++) {
            if (i >= 100) break; // 取前100条数据
            let item = {};
            item.value = res.data.dataSource[i].id;
            item.title = res.data.dataSource[i].shortName;
            item.id = res.data.dataSource[i].id;
            dataSource.push(item);
          }
          callback(dataSource);
        }
      }
      //银行间交易对手模块
      if (payload.dataType == 3) {
        delete payload.dataType;
        delete payload.isOtc;
        delete payload.scenesType;
        const res = yield call(api.counterpartyInformation.queryBankList, payload);
        if (res && res.code == 0) {
          let dataSource = [];
          for (let i = 0; i < res.data.length; i++) {
            if (i >= 100) break; // 取前100条数据
            let item = {};
            item.value = res.data[i].id;
            item.title = res.data[i].name;
            item.id = res.data[i].id;
            dataSource.push(item);
          }
          callback(dataSource);
        }
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
